package com.future.direction.ui.activity;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.SpanUtils;
import com.future.direction.R;
import com.future.direction.common.util.StatusBarUtil;
import com.future.direction.data.bean.UserInfoBean;
import com.future.direction.di.component.AppComponent;
import com.future.direction.di.component.DaggerLoginComponent;
import com.future.direction.di.module.LoginModule;
import com.future.direction.presenter.LoginPresenter;
import com.future.direction.presenter.contract.LoginContract;

import butterknife.BindView;

/**
 * Created by xwl on 2020/02/10
 * 登录页
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {
    @BindView(R.id.tv_agreement)
    TextView tvAgreement;
    @BindView(R.id.tv_verification_code)
    TextView tvVerificationCode;
    @BindView(R.id.tv_wx_login)
    TextView tvWxLogin;
    @BindView(R.id.tv_look_around)
    TextView tvLookAround;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;

    private String flag;//是否是第一次打开app "0"第一次 "1" 非第一次


    @Override
    public int setLayout() {
        return R.layout.activity_login;

    }
    @Override
    public void getExtras() {
        Intent intent = getIntent();
        flag = intent.getStringExtra("flag");
    }

    @Override
    public boolean init() {
        //测量手机状态栏的高度
        int statusBarHeight = StatusBarUtil.getStatusBarHeight(this);
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.setMargins(0, statusBarHeight, 0, 0);
        llTitle.setLayoutParams(lp);
        //判断是否是第一次打开app  控制返回按钮和随便看看的显示隐藏
        switch (flag) {
            case "0"://第一次打开app
                ivBack.setVisibility(View.GONE);
                tvLookAround.setVisibility(View.VISIBLE);
                break;
            case "1"://非第一次打开app
                ivBack.setVisibility(View.VISIBLE);
                tvLookAround.setVisibility(View.GONE);
                break;
        }

        //用户协议
        SpanUtils spanUtils = new SpanUtils().append("登录/注册表示同意").setFontSize(12, true).setForegroundColor(getResources().getColor(R.color.colorWhite))
                .append("用户协议 隐私协议").setFontSize(12, true).setForegroundColor(getResources().getColor(R.color.colorBlue0D7EF9)).setUnderline().setClickSpan(new ClickableSpan() {
                    @Override
                    public void onClick(@NonNull View widget) {
                        //点击使用协议
                    }
                });
        tvAgreement.setText(spanUtils.create());
        return true;
    }



    @Override
    public void setListener() {
        //验证码登录
        tvVerificationCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goVerificationCode();
            }
        });
        //微信登录
        tvWxLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goWXLogin();
            }
        });
        //返回按钮
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //随便看看
        tvLookAround.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goMain();
            }
        });

    }



    @Override
    public void setupAcitivtyComponent(AppComponent appComponent) {

        DaggerLoginComponent.builder().appComponent(appComponent)
                .loginModule(new LoginModule(this))
                .build().inject(this);
    }


    @Override
    public void loginSuccess(UserInfoBean userInfoBean) {
        mPresenter.saveInfo(userInfoBean);
        goMain();
    }

    private void goMain() {
        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
    //微信登录
    private void goWXLogin() {
        Intent intent = new Intent(LoginActivity.this, BindPhoneTwoActivity.class);
        startActivity(intent);
        finish();
    }
    //验证码登录
    private void goVerificationCode() {
        Intent intent = new Intent(LoginActivity.this, VerificationCodeLoginActivity.class);
        startActivity(intent);
        finish();
    }



}
